package servlets;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import myJDBC.lowLevel.Objects;
import myJDBC.lowLevel.Params;
import myJDBC.lowLevel.References;;
/**
 * Servlet implementation class SrvlCreateObject1
 */
@WebServlet("/SrvlCreateObject")
public class SrvlCreateObject extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = (String) request.getParameter("name");
		String description = request.getParameter("descr");
		Integer objTypeId= Integer.parseInt(request.getParameter("objTypeId"));
		Integer objParId=Integer.parseInt(request.getParameter("objParId"));
		//создаем объект
		int created=-1;
		try {
			created = Objects.createObject(name, description, objTypeId, objParId);
		} catch (SQLException e) {
			e.printStackTrace();
		}		
		//получаем значения аттрибутов
		String type="";
		Enumeration Names = request.getParameterNames();
		while(Names.hasMoreElements()) {
			  String id = (String)Names.nextElement();
			  if(checkString(id)){//если это номер атрибута
				  type=(String) request.getParameter("t"+id);				  
				  // каждому типу свой обработчик есть тип атрибута, айди атрибута, значение атрибута
				  if(type.equals("varchar2")||(type.equals("number"))) {
					String val=(String) request.getParameter(id);
					Params.createParamVarchNumb(Integer.parseInt(id), created, val);
				}
				  if(type.equals("date")) {
					String val=(String) request.getParameter(id);
					Params.createParamDate(Integer.parseInt(id), created, val);
				}
				  if(type.equals("array")) {
					String val=(String) request.getParameter(id);
					Params.createParamArray(Integer.parseInt(id), created, Integer.parseInt(val));
				}
				  if(type.equals("reference"))
						try {
							if(request.getParameterValues(id)!=null)
								for(String s:request.getParameterValues(id)){
									References.createReference(Integer.parseInt(id), created, Integer.parseInt(s));
								}
						} catch (SQLException e) {
							e.printStackTrace();
						}
				  
			  }
		}
		response.sendRedirect("createObject");
	}
	
	private boolean checkString(String string) {
        if (string == null || string.length() == 0) return false;

       char c = string.charAt(0);
       if (c >= '0' && c <= '9')
                return true;        
       return false;
    }
	
	/*private void showParams(HttpServletRequest request, HttpServletResponse response) throws IOException{
		PrintWriter pw = response.getWriter();
		  pw.println("<html><head><title>" +
		  "</title>" +
		  
		  "<table border=1 align=center>\n" +
		  "<TR bgcolor=\"#8AEAF4\">\n" +
		  "<td>Parameter Name</td>"+
		  "<td>Parameter Value(s)</td>\n");
		  Enumeration Names = request.getParameterNames();
		  while(Names.hasMoreElements()) {
		  String str = (String)Names.nextElement();
		  pw.println("<tr><td>" + str + "</td><td>");
		  String[] Values = request.getParameterValues(str);
		  if (Values.length == 1) {
		  String paramValue = Values[0];
		  if (paramValue.length() == 0)
		  pw.print("<I>No Value</I>");
		  else
		  pw.print(paramValue);
		  }
		  else {
		  pw.println("<UL>");
		  for(int i=0; i<Values.length; i++) {
		  pw.println("<LI>" + Values[i]);
		  }
		  pw.println("</UL>");
		  }
		  }
		  pw.println("</td></tr></table>\n</body></html>");
	}*/

}
